package com.example.jwt.controller;

import com.example.jwt.po.User;
import com.example.jwt.util.JWTUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class JWTController {
    @GetMapping("/get")
    public String creatToken2(){
        User user = new User();
        user.setId("1");
        user.setUsername("zhangsan");
        user.setPassword("123456");
        return JWTUtil.createJwt(40000,user);
    }

    @GetMapping("/test")
    public String testToken2(HttpServletRequest request, HttpServletResponse response){
        String token= request.getHeader("Authorization");
        User user = new User();
        user.setId("1");
        user.setUsername("zhangsan");
        user.setPassword("123456");
        if (JWTUtil.isVerify(token,user)){
            return "success";
        }
        return "fail";
    }
}
